﻿#include "USettingsPage.h"
#include "UToolBarManager.h"
#include "Global.h"
#include "CDragPreviewWidget.h"
#include "UToolBarManager.h"
#include <qapplication.h>
#include <qscreen.h>
USettingsPage::USettingsPage(QWidget *parent)
	: QWidget(parent)
{
	m_pWidget = new QWidget();
	ui.setupUi(this);
	QList<int> sizes;
	sizes << 230 << 1380;
	ui.splitter->setSizes(sizes);
	ui.stackedWidget->installEventFilter(this);
	ui.stackedWidget->setCurrentIndex(1);
	connect(ui.widget_Scene, SIGNAL(sign_Show(int)), this, SLOT(slot_ShowPage(int)));
	connect(ui.widget_Carouse, SIGNAL(sign_Show(int)), this, SLOT(slot_ShowPage(int)));
	connect(ui.widget_CarousePage, SIGNAL(sign_enlarge(bool)), this, SLOT(slot_enlarge(bool)));
	m_pSceneManager = ui.widget_Scene;
	m_pCarouselManager = ui.widget_Carouse;
}
void USettingsPage::refushUi()
{
	ui.widget_ScenePage->buildComBox();
	m_pSceneManager->refushUi();
	m_pCarouselManager->refushUi();
}

void USettingsPage::refushCombox()
{
	ui.widget_ScenePage->buildComBox();
}

void USettingsPage::setRootNode(CBaseNode * root)
{
	ui.widget_ScenePage->setRootNode(g_pRoot);
	//将UDropCellWidget传给USceneManager
	ui.widget_Scene->setDropCellWidget(ui.widget_ScenePage->getDropWidget());  
	//将UCarousePage传给UCarouselManager
	ui.widget_Carouse->setCarouseWidget(ui.widget_CarousePage);
}

USettingsPage::~USettingsPage()
{
}

bool USettingsPage::eventFilter(QObject* obj, QEvent* event)
{
	if (event->type() == QEvent::MouseButtonRelease)
	{
		if (obj == ui.stackedWidget)
		{
			QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
			if (mouseEvent->button() == Qt::LeftButton)
			{
				int nIndex = ui.stackedWidget->currentIndex();
				if (nIndex == 0)
				{
					g_pToolBarMang->setToolBtn(UToolBarManager::Scene, 2);
				}
				else
				{
					g_pToolBarMang->setToolBtn(UToolBarManager::Carouse, 2);
				}
				return true;
			}
		}
	}
	return QWidget::eventFilter(obj, event);
}

void USettingsPage::initUi()
{
	ui.splitter->widget(0)->setMinimumWidth(20); // 左边最小 200
	ui.splitter->widget(1)->setMinimumWidth(20); // 右边最小 300
	ui.splitter->setCollapsible(0, false);
	ui.splitter->setCollapsible(1, false);

	ui.splitter_2->widget(0)->setMinimumHeight(20); // 左边最小 200
	ui.splitter_2->widget(1)->setMinimumHeight(20); // 右边最小 300
	ui.splitter_2->setCollapsible(0, false);
	ui.splitter_2->setCollapsible(1, false);
	ui.widget_Carouse->initUi();
	ui.widget_Scene->initUi();
}

void USettingsPage::clickLog()
{
	int nIndex = ui.stackedWidget->currentIndex();
	if (nIndex == 0)
	{
		g_pToolBarMang->setToolBtn(UToolBarManager::Scene, 2);
	}
	else
	{
		g_pToolBarMang->setToolBtn(UToolBarManager::Carouse, 2);
	}
}

void USettingsPage::slot_enlarge(bool bEnlarge)
{
	if (bEnlarge)
	{
		ui.stackedWidget->removeWidget(ui.widget_CarousePage);
		ui.widget_CarousePage->setParent(m_pWidget);
		m_pWidget->showFullScreen();
		ui.widget_CarousePage->setGeometry(m_pWidget->rect());
		ui.widget_CarousePage->show();
		m_pWidget->setProperty("fullScreen", true); // 设置属性
		m_pWidget->style()->unpolish(this);  // 重新加载样式
		m_pWidget->style()->polish(this);
	}
	else
	{
		m_pWidget->hide();
		ui.widget_CarousePage->setProperty("fullScreen", false); // 设置属性
		ui.widget_CarousePage->style()->unpolish(this);  // 重新加载样式
		ui.widget_CarousePage->style()->polish(this);
		ui.widget_CarousePage->setParent(NULL);
		ui.stackedWidget->addWidget(ui.widget_CarousePage);
		ui.stackedWidget->setCurrentIndex(1);
	}

}

void USettingsPage::slot_ShowPage(int nIndex)
{
	ui.stackedWidget->setCurrentIndex(nIndex);
}

